<template>
  <div>
    <BasicTable
      @register="registerTable"
      @resize-column="handleResizeColumn"
      :canResize="false"
      :scroll="{ x: 'max-content' }"
    >
      <template #toolbar>
        <div style="margin-right: 20px">
          合计: {{ sumData }}元
        </div>
        <a-button type="primary" @click="handleExport">充值日报表导出</a-button>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
  name: 'CompanyRecharge',
});
</script>
<script lang="ts" setup>
import { ref } from 'vue';
import { BasicTable, useTable } from '@/components/Table';
import { companyRechargeColumns, companyRechargeSearch } from '../data';
import { useMessage } from '@/hooks/web/useMessage';
import { defHttp } from '@/utils/http/axios';
import { useRoute } from 'vue-router';
import { downloadBlob } from '@/utils/file/download';
const { createMessage } = useMessage();
const router = useRoute();
const sumData = ref();
const [registerTable, { reload, getForm, getSelectRows, getSelectRowKeys }] =
  useTable({
    //title: '列表',
    api: '/admin/recharge_manager/companyRecharge',
    columns: companyRechargeColumns,
    showIndexColumn: true,
    useSearchForm: true,
    indexColumnProps: {
      fixed: 'left',
    },
    beforeFetch: (info) => {
      if (router.query.companyId) {
        return { ...info, companyId: router.query.companyId };
      }
      return;
    },
    formConfig: {
      labelWidth: 80,
      hideLabel: true,
      schemas: companyRechargeSearch,
      // baseColProps: { span: 6 },
    },
    pagination: true,
    bordered: true,
    rowKey: 'sn',
    afterFetch: () => {
      const queryString = Object.keys(getForm().getFieldsValue())
        .map(
          (key) =>
            `${encodeURIComponent(key)}=${encodeURIComponent(getForm().getFieldsValue()[key])}`,
        )
        .join('&');
      defHttp.get({ url: '/admin/recharge_manager/companyRechargeSum?'+queryString})
        .then(res => {
          sumData.value = res.sumAmount;
        })
    },
    rowSelection: {
      type: 'checkbox',
    },
  });
const exportLoading = ref(false);
const handleExport = async () => {
  debugger
  try {
    exportLoading.value = true;
    let param;
    if (getSelectRows().length > 0) {
      const ids = [];
      for (let a = 0; a < getSelectRows().length; a++) {
        ids.push(getSelectRows()[a].id);
      }
      debugger
      param = {
        ids: ids,
        importType: 'checkBox',
      };
    } else {
      param = {
        ...getForm().getFieldsValue(),
      };
    }
    const res = await defHttp.post(
      {
        url: '/admin/recharge_manager/companyRechargeExcel',
        responseType: 'blob',
        data: JSON.stringify(param),
      },
      { isReturnNativeResponse: true },
    );
    downloadBlob(res);
  } catch (error) {
    console.log(error);
  } finally {
    exportLoading.value = false;
  }
};
function handleSuccess() {
  reload();
}
</script>
